package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.dto.UserRoleDTO;
import cn.edu.sgu.www.pms.entity.Role;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.UserRoleService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "用户角色管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/user_role", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class UserRoleController {

    private final UserRoleService userRoleService;

    @Autowired
    public UserRoleController(UserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }

    @ApiOperation("添加用户角色")
    @RequestMapping(path = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated UserRoleDTO userRoleDTO) {
        userRoleService.insert(userRoleDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("删除用户角色")
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    public JsonResult<Void> delete(@Validated UserRoleDTO userRoleDTO) {
        userRoleService.delete(userRoleDTO);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("查询用户的角色列表")
    @RequestMapping(path = "/selectByUserId", method = RequestMethod.GET)
    public JsonResult<List<Role>> selectByUserId(
            @ApiParam(name = "userId", value = "用户ID", required = true)
            @NotBlank(message = "用户" + CustomMessages.ID_NOT_NULL) String userId) {
        List<Role> list = userRoleService.selectByUserId(userId);

        return JsonResult.success(null, list);
    }

}